package org.ams.common.util;

import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

public class ContextUtil {

	private static WebApplicationContext context;
	
	public static Object getBean(String name) throws Exception {
		Object obj = null;
		if(name != null) {
			WebApplicationContext c = getContext();
			if(c == null) {
				for(int i=0; i<60; i++) {
					Thread.sleep(1000);
					c = getContext();
					if(c != null)
						break;
				}
			}
			obj = c.getBean(name);
		}
		return obj;
	}
	
	private static WebApplicationContext getContext() {
		if(context == null)
			context = ContextLoader.getCurrentWebApplicationContext();
		return context;
	}
}
